using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace ScytheEngine
{
    public delegate void UpdateMethod();
    public class ButtonList : Collection<ButtonState> {}
    public class KeyList : Collection<Keys> {}

    public class Input : GameComponent
    {
        #region Data Members

        private Point m_LastMouseLocation;
        private Vector2 m_MouseMoved;
        private ButtonList m_MouseButtons;
        private KeyList m_Keys;
 
        public UpdateMethod OnUpdate;

        #endregion

        #region Ctors

        public Input(ScytheEngineGame game)
            : base(game)
        {

        }

        #endregion

        #region Methods

        public override void Initialize()
        {
            m_MouseButtons = new ButtonList();
            m_Keys = new KeyList();

            MouseState startState = Mouse.GetState();
            m_LastMouseLocation = new Point(startState.X, startState.Y);

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            MouseState state = Mouse.GetState();

            m_MouseMoved = new Vector2(m_LastMouseLocation.X - state.X, m_LastMouseLocation.Y - state.Y);

            m_LastMouseLocation = new Point(state.X, state.Y);

            m_MouseButtons.Clear();
            m_MouseButtons.Add(state.LeftButton);
            m_MouseButtons.Add(state.MiddleButton);
            m_MouseButtons.Add(state.RightButton);
            m_MouseButtons.Add(state.XButton1);
            m_MouseButtons.Add(state.XButton2);

            m_Keys.Clear();
            foreach( Keys k in Keyboard.GetState().GetPressedKeys())
            {
                m_Keys.Add(k);
            }

            if(OnUpdate != null)
            {
                OnUpdate();
            }

            base.Update(gameTime);
        }

        #endregion

        #region Properties

        public Vector2 MouseMoved { get { return m_MouseMoved; } }
        public ButtonList MouseButtons { get { return m_MouseButtons; } }
        public KeyList Keys { get { return m_Keys; } }

        #endregion
    }
}
